﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Order.MicroService.Api.Config;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Order.MicroService.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MethodsController : ControllerBase
    {
        private readonly IHttpClientFactory _httpClientFactory;

        private readonly HttpClientService _httpClientService;

        public MethodsController(IHttpClientFactory httpClientFactory, HttpClientService httpClientService)
        {
            _httpClientFactory = httpClientFactory;
            _httpClientService = httpClientService;
        }

        [HttpGet]
        public IActionResult Get()
        {
            return Ok("Hello World");
        }


        [HttpGet("TestGetMethodWithValues")]
        public async Task<string> TestGetMethodWithValues(Dictionary<string, string> parameters, string token)
        {
            var result = string.Empty;
            var response = await _httpClientService.TestGetMethodWithValues(parameters, token);
            //response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
            }
            return result;
        }
        
        [HttpPost("GetInfoByStringContent")]
        public async Task<string> GetInfoByStringContent(Dictionary<string, string> parameters)
        {
            var result = await _httpClientService.GetInfoByStringContent(parameters);      
            return result;
        }

        [HttpPost("GetInfoByJson")]
        public async Task<string> GetInfoByJson(Dictionary<string, string> parameters)
        {
            var result = await _httpClientService.GetInfoByJson(parameters);
            return result;
        }

        [HttpPost("GetInfoByFormData")]
        public async Task<string> GetInfoByFormData([FromForm] string name, [FromForm] string Id)
        {
            var result = await _httpClientService.GetInfoByFormData(name, Id);
            return result;
        }

        [HttpGet("TestPolly")]
        public async Task<string> TestPolly()
        {
            var result = await _httpClientService.TestPolly();
            return result;
        }

        [Authorize]
        [HttpGet("GetOrderInfo")]
        [Produces("application/json")]
        public string GetOrderInfo()
        {
            return "woaini";
        }
    }
}
